import 'dart:convert';

void main() {
  //创建一个User对象
  User user = User(name: '张三', age: 18, sex: '男');
  print('姓名：${user.name},年龄：${user.age},性别：${user.sex}');

  //实际开发中
  //2、调用接口获取的Json数据
  String jsonStr = '{"name":"李四","age":10,"sex":"女"}';
  //将json数据转为Map对象
  Map mapData = json.decode(jsonStr);
  print('姓名:${mapData['name']},年龄:${mapData['age']},性别：${mapData['sex']}');

  //3、创建一个User对象
  User user2 = User.formMap(mapData);
  print('姓名:${user2.name},年龄:${user2.age},性别：${user2.sex}');
}

class User {
  String? name;
  int? age;
  String? sex;

  //主构造函数
  User({required this.name, required this.age, required this.sex});

  //次构造函数
  User.formMap(Map map) {
    this.name = map['name'];
    this.age = map['age'];
    this.sex = map['sex'];
  }
}
